<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/user.jsp"%>
<%@ include file="/common/sms.jsp"%>
<%@ include file="/common/huanxin.jsp"%>
<%!
	static final List<String> sources = new ArrayList<String>(Arrays.asList("qq", "wechat", "weibo"));
%>
<%
	String source = input.getDesc("source");
	String unionId = input.getDesc("union_id");
	Integer type = input.getInt("type");
	// 第三方登录，如果已经绑定过帐号则直接登录，否则生成随机用户名新创建帐号后再登录,同时创建环信帐号
	if (sources.contains(source)) {
		List<Map> list = QBuilder.db("basedb").select("new map(u.id as id, u.userName as user_name, u.password as password,u.hospital as hospital)").from("UserAccount as ua, User as u").where(new Where("ua.uid=u.id").and("ua.source=", source).and("ua.unionId=", unionId).and("ua.hospital=", hospital).and("ua.status=", STATUS_ENABLE).and("u.type=", type).and("u.status=", STATUS_ENABLE).and("u.hospital=", hospital)).query().getResultObj();
		if (isEmpty(list)) {
			if (type.equals(USER_EMPLOYEE)) {
				Tools.encryptPrint(out, new Message(40001, "您尚未绑定账户，请使用账号登录后绑定"));
				return;
			}
			// 核对第三方token及openid合法性
			String accessToken = input.getDesc("access_token");
			switch (source) {
				case "wechat": {
					String res = HttpUtil.get("https://api.weixin.qq.com/sns/auth?access_token=" + accessToken + "&openid=" + unionId);
					Map wechatMap = JSONUtil.fromJson(res, new TypeReference<Map>(){});
					if ((Integer)wechatMap.get("errcode") != 0) {
						Tools.encryptPrint(out, new Message(40001, "微信第三方登录失败,token无效"));
						return;
					}
				}
				break;
				case "qq": {

				}
				break;
				case "weibo": {

				}
				break;
			}
			User user = User.fromMap(parameters, User.class);
			String avatar = input.getDesc("avatar", "");
			user.setUserName(generateUserName(source));
			user.setPassword(Tools.getMD5("123456"));
			user.setRepassword(Tools.getMD5("123456"));
			UserProfile userProfile = UserProfile.fromMap(parameters, UserProfile.class);
			msg = register(user, userProfile);
			if (msg.getErrcode() == 0) {
				Map uidMap = (Map)msg.get("data");
				Integer uid = (Integer)uidMap.get("uid");
				Integer pid = (Integer)uidMap.get("pid");
				String app = input.getDesc("application", "");
				if (!"".equals(app) && msg.getErrcode() == 0) {
					register(uid + "", "123456", app, hospital);
				}
				UserAccount userAccount = new UserAccount();
				userAccount.setUid(uid);
				userAccount.setPid(pid);
				userAccount.setHospital(hospital);
				userAccount.setUnionId(unionId);
				userAccount.setSource(source);
				DBTools.db("basedb").addItem(userAccount);
			} else {
				Tools.encryptPrint(out, msg);
				return;
			}
		}
		Tools.encryptPrint(out, login(source, unionId, type, hospital));
		return;
	}
	String phone = input.getDesc("phone", "");
	// 手机号快捷登录
	if (!"".equals(phone)) {
		String code = input.getDesc("code");
		Message codeMsg = validateSms(phone, code, LOGIN_SMS, hospital);
		if (codeMsg.getErrcode() != 0) {
			Tools.encryptPrint(out, codeMsg);
			return;
		}
		List<User> users = QBuilder.db("basedb").select().from("User").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("type=", type).and("phone=", phone)).query().getResultObj();
		if (isEmpty(users)) {
			Tools.encryptPrint(out, new Message(30041, "手机号未注册"));
			return;
		}
		// 如果未绑定账号，是否应自动注册并登录？
		User user = users.get(0);
		Tools.encryptPrint(out, login(user));
		return;
	}
	User user = User.fromMap(parameters, User.class);
	user.setType(isEmpty(user.getType()) ? USER_PATIENT : user.getType());
	msg = login(user);
	Tools.encryptPrint(out, msg);
%>
